<?php
namespace app\jincheng\controller;
use think\Db;
use think\Controller;
use think\Log;
use app\service\jinchengService;
use app\service\billService;
use app\service\xcxService;
use app\service\apixcxService;
class Applet extends Controller{
    public function formIdAdd(){
        
        $applet_form["service_id"]=6;//金城小程序
        $applet_form["ctime"]=time();
        $applet_form["user_id"]=request()->get("user_id");
        $applet_form["form_id"]=request()->get("formId");
        $applet_form["num"]=3;
        $res=Db::table("applet_form")->insert($applet_form);
    }
    public function contract(){
        $user_id=request()->post("user_id");
        if(strlen($user_id)!="16"){
            return json(["errCode"=>"1",'errmsg' => '请绑定房屋']);
        }
        $roomuser=Db::table("roominfo_user")->where("user_id",$user_id)->find();
        if(empty($roomuser)){
            return json(["errCode"=>"1",'errmsg' => '请绑定房屋']);
        }
        $communityinfo=Db::table("community")->where("community_id",$roomuser["community_id"])->find();
        return json(["errCode"=>"0",'phone' => $communityinfo["hotline"]]);
    }
    public function getimg(){
        $service_id=6;
        $imglist=Db::table("applet_carousel")->where("service_id",$service_id)->select();
        return json_encode($imglist);
    }
    //获取支付宝用户的user_id
    public function userId()
    {
        $api = new jinchengService();
        //获取当前车辆支付宝ID
        $code = request()->get('auth_code');
        $oauthtoken = $api->xcxoauthToken($code);
       
        if(property_exists($oauthtoken,'sub_msg')){
            return json(['info' => $oauthtoken->sub_msg]);
        }
        $user_id = $oauthtoken->user_id;//user_id
        return json(['info' => $user_id]);
        return json_encode($user_id);
    }
    //提交信息计算物业费
    public function details()
    {
        //根据小区和完整门牌号查询房屋信息
        $room_id=request()->post("room_id");
        $roominfo = Db::table("roominfo")->where("id",$room_id)->find();
        //Log::write($roominfo,"122");
        //根据房号查询账单
        $billlis = (new xcxService())->billRoomIdList($roominfo['out_room_id']);
        $billlist = (new xcxService())->cost_type($roominfo['out_room_id']);
         
        foreach ($billlist as $b){
            $lis[] = (new xcxService())->cost_type_bill($roominfo['out_room_id'],$b);
        }
        //$lis = array_unique($lis);
        foreach($billlis as $list){
            $info['acct_period'][] = $list['acct_period'];
            $info['cost_type'][] = $list['cost_type'];
            $info['bill_amount'][] = $list['bill_entry_amount'];
            $info['id'][] = $list['id'];
        }
        if(empty($info)){
            return json(['error' => '暂无可缴费账单']);
        }
        return json_encode($lis);

    }
   
    //确认订单
    public function order(){
        $data=request()->post();
        $user_id =  $data['user_id'];
        $id = $data['id'];

        if(empty($id)){
            return json(['errorCode'=>'1','error' => '请选择要缴费的账单']);
        }

        //$id = array_unique($data['id']);
        //根据账单编号查询账单金额
        $infolist = (new billService())->billEntryIdInfo($id);
        
        if(count($infolist) != count($id)){
            return json(['errorCode'=>'1','info' => '账单信息不完整']);
        }

        $out_room_id="";
        $room_address="";
        $community_id="";
        foreach($infolist as $info){
            //查询小区名称
            $community_name = (new billService())->communityMachine($info['community_id'])['community_name'];
            $bill_entry_amount[] = $info['bill_entry_amount'];
            $out_room_id = $info['out_room_id'];
            $community_id = $info['community_id'];
            $room_address = $info['room_address'];
        }

        $out_trade_no = time().rand(100000,999999);
        $bill_amount_sum = array_sum($bill_entry_amount);
        
        $roominfo=Db::table("roominfo")->where("out_room_id",$out_room_id)->find();
        $subject = $community_name.$room_address.'-'.$roominfo["owner_name"].'物业缴费';
         
        //查询token
        $token = (new xcxService())->token($community_id);
        //Log::write($token,"token");
        $api = new apixcxService();
        $notifyUrl="https://www.jfzhiwen.com/admin/callback/alinotice";
        $result = $api->create(json_encode(['out_trade_no'=> $out_trade_no,'total_amount'=>$bill_amount_sum,'buyer_id'=>$user_id,'subject' =>$subject,'token'=>$token ,'notifyUrl'=>$notifyUrl]));
        
        if(property_exists($result,'sub_msg')){
            return json(['errorCode'=>'1','info' => $result->sub_msg]);
        }else{
            $close_id = $id['0'];
            $is_trade_no = Db::name('bill')->where('id',$close_id)->value('trade_no');
            if(!empty($is_trade_no)){
                
                $aaa = $api->close(json_encode(['trade_no' => $is_trade_no,'token' => $token]));
                if(property_exists($aaa,'sub_msg')){
                   Db::name('test')->insert(['test'=>$aaa->sub_msg]);
                   Db::name('test')->insert(['test'=>$is_trade_no]);
                }
                
            }
            //添加进数据库
            $bill['trade_no'] =  $result->trade_no;
            $bill['out_trade_no'] = $out_trade_no;
            $bill['user_id'] = $user_id;
            foreach($id as $billid){
                $bill['id'] = $billid;
                $billedit = (new billService())->billEdit($bill);
                if(empty($billedit)){
                    return json(['errorCode'=>'1','info' => '账单存储失败']);
                }
            }
            $billpay["user_id"]=$user_id;
            $billpay["order_money"]=$bill_amount_sum;
            $billpay["pay_money"]=$bill_amount_sum;
            $billpay["out_trade_no"]= $out_trade_no;
            $billpay["trade_no"]= $bill['trade_no'];
            $billpay["billid"]=serialize($id);
            $billpay["out_room_id"]=$out_room_id;
            $billpay["room_address"]=$room_address;
            $billpay["community_name"]=$community_name;
            $billpay["community_id"]=$community_id;
            $billpay["scenes"]=2;
            $res=Db::table("billpay")->insert($billpay);
            if($res){
                //返回结果到前台唤起收银台
                return json(['trade_no' => $bill['trade_no']]);
            }else{
                return json(['errorCode'=>'1','info' => '账单存储失败']);
            }
            
        }
    }

} 